<template>
  <div class="about-global">
    <c-header :active="type" @changeType="getType"></c-header>
    <div class="title" v-if="productType == 0">产品中心-强化复合地板</div>
    <div class="title" v-else>产品中心-实木复合地板</div>
    <div class="product-type">
      <div class="product-type-logo">
        <img v-if="productType == 0" src="@/assets/img/product_type01.png" alt />
        <img v-if="productType == 1" src="@/assets/img/product_type02.png" alt />
      </div>
      <div class="product-type-sign">
        <div v-show="productType == 0">
          <span class="black"></span>
          <span>殿堂版</span>
        </div>
        <div v-show="productType == 0">
          <span class="orange"></span>
          <span>典藏版</span>
        </div>
        <div v-show="productType == 0">
          <span class="green"></span>
          <span>乐活版</span>
        </div>
        <div>
          <span class="stock"></span>
          <span>有库存</span>
        </div>
      </div>
      <div class="product-title">
        <p class="product-name">{{ productTitle.name }}</p>
        <p class="product-specs">{{ productTitle.specs }}</p>
      </div>
      <div class="product-list">
        <div class="product-li" v-for="(item, index) in productList" :key="index">
          <img class="product-img" :src="item.imgurl" :alt="item.product_name" @click.stop="checkImg(item.image)" />
          <p class="product-desc">
            <span class="wid">{{ item.product_name }}</span>
            <!-- <van-icon
              name="shopping-cart-o"
              class="shoppingcart-icon"
              v-if="type == 1"
              @click.stop="addCart(item.id)"
            /> -->
            <img class="shoppingcart-icon" src="http://pergo.weilang.top/index/img/shoppingcart01.png" alt="" v-if="type == 1"  @click.stop="addCart(item)" />
          </p>
          <p>
            <span :class="classObject(item.version)" v-show="productType == 0"></span>
            <span>{{ item.code }}</span>
            <span v-if="item.stock == 1" class="stock"></span>
          </p>
        </div>
        <div class="tips">殿堂版与典藏版产品详询门店经理</div>
      </div>
    </div>
    <van-image-preview v-model="show" :images="dialogImgArr"></van-image-preview>
    <div class="shoppingCart" v-if="type == 1">
      <router-link :to="{ path: '/reserve', query: { type: 1 } }">
        <img src="@/assets/img/cart.png" alt />
        <p>预定>></p>
      </router-link>
    </div>
    <c-footer :type="type" @changeType="getType"></c-footer>
  </div>
</template>

<script>
import { getGoods, applyProductAdd } from '@/api/index';
import { XDialog } from 'vux';
import Swiper from '@/components/Swiper/Swiper';
import { mapGetters } from 'vuex';
export default {
  components: {
    XDialog,
    Swiper
  },
  data() {
    return {
      productType: this.$route.query.productType ? this.$route.query.productType : 0,
      id: this.$route.query.id,
      productList: [],
      productTitle: {},
      show: false,
      dialogImgArr: []
    };
  },
  created() {
    this.$vux.loading.show({
      text: 'Loading'
    });
    let obj = {
      page: 1,
      limit: 12,
      cate_id: this.id
    };
    getGoods(obj).then(res => {
      const { code, msg, data } = res;
      if (code == 200) {
        this.productList = data.rows;
        this.productTitle = data.cate_row;
        setTimeout(() => {
          this.$vux.loading.hide();
        }, 500);
      }
    });
  },
  computed: {
    ...mapGetters(['type']),
    classObject: function() {
      return function(version) {
        return {
          black: version == 1,
          orange: version == 2,
          green: version == 3
        };
      };
    }
  },
  methods: {
    getType(type) {
      this.$store.dispatch('setType/getType', type);
    },
    checkImg(imgArr) {
      this.dialogImgArr = [];
      this.dialogImgArr = imgArr;
      this.$vux.loading.show({
        text: 'Loading'
      });
      setTimeout(() => {
        this.$vux.loading.hide();
        this.show = true;
      }, 500);
    },
    addCart(item) {
      this.$vux.toast.text("添加成功,请在购物车查看", "center");
      let is_had = false;

      var productList = JSON.parse(localStorage.getItem('productList')) || [];
      if (productList.length == 0) {
        productList.push({
          id: item.id,
          imgurl: item.imgurl,
          product_name: item.product_name,
          code: item.code,
          producttype: item.producttype,
          is_select:false,
          num: 1
        });
      } else {
        productList.forEach((item1, index) => {
          if (item1.id == item.id) {
            is_had = true;
            item1.num++;
          }
        });

        if (!is_had) {
          productList.push({
            id: item.id,
            imgurl: item.imgurl,
            product_name: item.product_name,
            code: item.code,
            producttype: item.producttype,
             is_select:false,
            num: 1
          });
        }
      }

      localStorage.setItem('productList', JSON.stringify(productList)); //存

      // pergoArray = JSON.parse(localStorage.getItem('pergoArray'));//取
      // applyProductAdd({
      //   product_id: id
      // }).then(res => {
      //   const { code, msg, data } = res;
      //   if (code != 200) {
      //     this.$vux.toast.text(msg, "top");
      //     this.$router.push({
      //       path: "/dealer"
      //     });
      //   } else if (code == 200) {
      //     this.$vux.toast.text("添加成功,请在购物车查看", "center");
      //   }
      // });
    }
  }
};
</script>

<style lang="less" scoped>
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.block {
  width: 100%;
}

.shoppingCart {
  position: fixed;
  bottom: 3.81rem;
  right: 0.21rem;
  background: #000000;
  text-align: center;
  padding: 0.18rem;

  a {
    display: block;
    font-size: 0.28rem;
    color: #fff;

    img {
      width: 0.54rem;
    }
  }
}

.swiper-container-product {
  height: 251px;
  .swiper-slide {
    width: 100% !important;
  }
}

.product-name {
  font-size: 0.32rem;
}

.product-title {
  margin: 0.27rem 0 0.36rem 0;
}

.product-list {
  display: flex;
  display: -webkit-flex;
  justify-content: space-between;
  flex-direction: row;
  flex-wrap: wrap;
}

.tips {
  width: 100%;
  margin: 0.36rem 0 0.54rem;
  text-align: center;
}

.product-specs {
  color: #999999;
}

.product-type {
  padding: 0.36rem 0.21rem;
}

.product-type-logo {
  margin-bottom: 0.09rem;
}

.product-type-logo img {
  width: 50%;
}

.product-li {
  width: 48%;
  margin-bottom: 0.25rem;

  img.product-img {
    width: 100%;
    height: 4.8rem;
  }

  p {
    font-size: 0.25rem;
  }

  .stock {
    margin-left: 0.36rem;
  }

  .product-desc {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .wid {
      display: block;
      width: 80%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .shoppingcart-icon {
      // width: 0.4rem;
      font-size: 0.4rem;
      color: #cd873f;
    }
  }
}

.product-type-sign {
  display: flex;
  padding-bottom: 0.27rem;
  border-bottom: 1px solid #eaeaea;

  > div {
    display: flex;
    align-items: center;
    margin-right: 0.36rem;
  }
}

.black {
  display: inline-block;
  width: 0.25rem;
  height: 0.25rem;
  background: #000;
  margin-right: 0.09rem;
}

.orange {
  display: inline-block;
  width: 0.25rem;
  height: 0.25rem;
  background: #dd8821;
  margin-right: 0.09rem;
}

.green {
  display: inline-block;
  width: 0.25rem;
  height: 0.25rem;
  background: #6fa027;
  margin-right: 0.09rem;
}

.stock {
  display: inline-block;
  width: 0.18rem;
  height: 0.18rem;
  background: #000;
  margin-right: 0.09rem;
  border-radius: 50%;
}
</style>
